<?php
include_once '../comic/WCPersistantObject.php';
include_once '../utils/WCRelation.php';

/*
 * Copyright 2009 Dan Klco
 * This file is part of Daily WebComics.
 * 
 * Daily WebComics is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Daily WebComics is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Daily WebComics.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * This object represents a single entry in a comic strip.  It has links back to the
 * comic strip object as well as links to multiple image objects, each being a single
 * rendition of the comic.
 * 
 * @author daniel.klco
 * @version 20090303
 *
 */
class WCComic extends WCPersistantObject{
	public static $ATTR_TITLE='title';
	public static $ATTR_DESCRIPTION='description';
	public static $ATTR_SRC='src';
	public static $ATTR_ALT='alt';
	public static $ATTR_XPATH_TO_NEWEST = 'xpath_to_newest';
	public static $ATTR_NEWEST_IMG_XPATH= 'newest_img_xpath';
	public static $ATTR_NEWEST_SRC_XPATH = 'newest_src_xpath';
	public static $ATTR_NEWEST_SRC_PREFIX = 'newest_src_prefix';
	public static $ATTR_NEWEST_SRC_SUFFIX = 'newest_src_suffix';
	
	private static $UPDATE_WEEKDAYS='WEEKDAYS';
	private static $UPDATE_DAILY='DAILY';
	private static $UPDATE_MWF='MWF';
	private static $UPDATE_W='W';
	private static $UPDATE_DISCONTINUED='DISCONTINUED';
	private static $UPDATE_INACTIVE='INACTIVE';
	private static $UPDATE_AD_HOC='AD_HOC';
	private static $UPDATE_SUNDAYS='SUNDAYS';
	
	public function loadComic(){
		if(!$this->newestOnHomepage){
			$s = new Scraper('','');
			$s->scrape($this->homepage);
			$page_links = $s->getValues($xpathToNewest);
			$s->free();
			$newest_link = $page_links[0];
		}else{
			$newest_link=$this->homepage;
		}
		$s->scrape($newest_link);

		//TODO: some error handling would be nice
		//TODO: should make method that returns a single value
		//get the src
		$srcs = $s->getValues($this->newestSrcXPath);
		$this->newestSrc=$srcs[0];
		
		//get the title
		$titles = $s->getValues($this->newestSrcXPath);
		$this->newestTitle=$titles[0];
		
		//get the img 
		$imgs = $s->getValues($this->newestImgXPath);
		$this->newestImg=$imgs[0];
		
		Images::downloadImage($this->newestSrcPrefix.$this->newestSrc.$this->newestSrcSuffix,Conf::$images_url."/$name/$name.jpg");
		//TODO: Figure out how I want to resize and crop the image here, I was thinking if it's tall resize on the width then crop on the hight and opposite if it's wide
		
	}
	
	/**
	 * Creates a new comic-image relationship between this object and the specified image.
	 * 
	 * @param $comicId
	 * @param $db
	 * @return none
	 */
	public function addImage($image){
		$this->createRelationship($image,WCRelation::$COMIC_IMAGE_RELATION);
	}
	
	/**
	 * Returns true if the object can be saved to the database.
	 * 
	 * @param true if the object is new eg. going to be inserted, false if it is going to be updated
	 * @return true if the object can be saved, false otherwise
	 */
	protected function canSave($new){
		if(!$new){
			$cond = ' AND NOT id='.$this->getId();
		}
		$query = $this->db->prepare('SELECT 1 FROM '.$this->table.' '.WCComic::$ATTR_SRC.'=\'?\''.$cond,array($this->getAttribute(WCComic::$ATTR_SRC)));
		$result = $this->db->executeQuery($query);
		if(mysql_num_rows($result) != 0){
			return false;
		}
		return true;
	}
	
	/**
	 * Builds a new WCComic object
	 * 
	 * @param $db
	 * @param $id
	 * @return unknown_type
	 */
	public function __construct($db,$id=NULL){
		parent::__construct('wc_comic',$db,$id);
	}
	
	/**
	 * Creates the table in the database for this object.  Passing in the delete flag as true will cause the function to
	 * delete the table if it already exists.  If this flag is false or omitted the function will throw an error if the 
	 * table already exists.
	 * 
	 * The columns for WCComic are:
	 * 	id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
	 * 	title VARCHAR(512)
	 * 	description TEXT
	 * 	src VARCHAR(512)
	 *  alt VARCHAR(512)
	 * 
	 * @param $delete if true the table will be deleted if it already exists
	 * @return none
	 */
	public function createTable($delete=false){
		parent::createTable($delete);
		$this->db->executeQuery('CREATE TABLE '.$this->table.' (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,title VARCHAR(512),description TEXT,src VARCHAR(512),alt VARCHAR(512))'); 
	}
	
	/**
	 * Returns the id of the strip the comic is associated with.
	 * 
	 * @return list of comic ids
	 */
	public function getComicStripId(){
		$comic_strip = $this->getRelated(WCRelation::$COMIC_STRIP_COMIC_RELATION);
		if(sizeof($comic_strip) != 1){
			throw new WCException(WCException::$CLASS_ERROR,"This comic entry is not associated with a comic strip.");
		}
		return array_pop(array_keys($comic_strip));
	}
	
	/**
	 * Returns a list of the of the images the comic is associated to
	 * 
	 * @return list of comic ids
	 */
	public function getImageIds(){
		return array_keys($this->getRelated(WCRelation::$COMIC_IMAGE_RELATION));
	}
	
	/**
	 * Returns all of the accepted attributes for the object.  These attributes will be saved to the database
	 * when the object is saved.  The key is the name of the attribute the value is whether or not it requires
	 * quotes.
	 * 
	 * The acceptable attributes for WCComic are:
	 * 	title
	 * 	description 
	 * 	src
	 *  alt
	 * 
	 * @return An array of the attributes for the object.
	 */
	public function objectAttributes(){
		return array('title'=>true,'description'=>true,'src'=>true,'alt'=>true);
	}
	
	/**
	 * Returns all of the required attributes for the object.  If these attributes are not set, the object
	 * will throw an exception while attempting to save.
	 * 
	 * The required attributes for WCComic are:
	 * 	src
	 * 
	 * @return An array of the required attributes for the object.
	 */
	public function requiredAttributes(){
		return array('src');
	}
}
?>